package cn.js.ccit.flowersmall.goldCoin.controller;

import cn.js.ccit.flowersmall.goldCoin.entity.GLDDetailEntity;
import cn.js.ccit.flowersmall.goldCoin.entity.GLDUserEntity;
import cn.js.ccit.flowersmall.goldCoin.service.GLDService;
import cn.js.ccit.flowersmall.goldCoin.service.impl.GiftGoldCoinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class GoldCoinController {
    @Resource
    GLDService gldService;
    @Autowired
    GiftGoldCoinService giftService;

    @GetMapping("/goldCoin")
    public Map<String, Object> goldCoin(String username) {
        // 获取当前登录用户
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof UserDetails){
            username = ((UserDetails)principal).getUsername();
        }else {
            username = principal.toString();
        }
        log.info("username: {}", username);

        GLDUserEntity gldUser= gldService.getGLDByUserName(username);
        List<GLDDetailEntity> gldDetails = gldService.findGLDDetails(username);
        Map<String, Object> result = new HashMap<>();
        result.put("goldCoin", gldUser);
        result.put("gldDetails", gldDetails);
        return result;
    }

    @PostMapping("/updateGoldCoin")
    public void updateGoldCoin(String userName, Integer goldCoin, String orderNo){
        giftService.updateGoldCoin(userName,goldCoin,orderNo);
    }

}
